<?php

namespace plugin\cms\app\bll;

use app\bll\ComponentExtend as BllComponentExtend;
use app\bll\ComponentExtendMethod as BllComponentExtendMethod;

use Exception;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use plugin\cms\app\validate\AipublishKeyword as ValidateAipublishKeyword;
use support\Db;

class AipublishKeyword
{

    /**
     * 获取关键词
     *
     * @param integer $id
     * @param integer $siteid
     * @return array
     */
    public function find(int $id, int $siteid): array
    {
        $keyword = Db::table('cms_aipublish_keyword')
            ->where('id', '=', $id)
            ->where('siteid', '=', $siteid)
            ->first();
        return $keyword === null ? [] : get_object_vars($keyword);
    }


    /**
     * 添加关键词
     *
     * @param array $data
     * @return integer
     */
    public function add(array $data): int
    {
        $ValidateAipublishKeyword = new ValidateAipublishKeyword();
        if (!$ValidateAipublishKeyword->check($data)) {
            throw new Exception($ValidateAipublishKeyword->getSimpleError());
        }

        list($extendid, $methodid) =  explode('_', $data['aimodel_nos']);
        $bllComponentExtend = new BllComponentExtend();
        $extendinfo = $bllComponentExtend->find($extendid);

        $bllComponentExtendMethod = new BllComponentExtendMethod();
        $methodinfo              = $bllComponentExtendMethod->find($methodid);

        $data['aimodel'] = $extendinfo['ident'] . '_' . $methodinfo['path'];

        return   Db::table('cms_aipublish_keyword')->insertGetId($data);
    }


    /**
     * 编辑关键词
     *
     * @param array $data
     * @param integer $id
     * @param integer $siteid
     * @return integer
     */
    public function edit(array $data, int $id, int $siteid): int
    {
        $ValidateAipublishKeyword = new ValidateAipublishKeyword();
        if (!$ValidateAipublishKeyword->check($data)) {
            throw new Exception($ValidateAipublishKeyword->getSimpleError());
        }

        list($extendid, $methodid) =  explode('_', $data['aimodel_nos']);
        $bllComponentExtend = new BllComponentExtend();
        $extendinfo = $bllComponentExtend->find($extendid);

        $bllComponentExtendMethod = new BllComponentExtendMethod();
        $methodinfo              = $bllComponentExtendMethod->find($methodid);

        $data['aimodel'] = $extendinfo['ident'] . '/' . $methodinfo['path'];

        return Db::table('cms_aipublish_keyword')
            ->where('id', '=', $id)
            ->where('siteid', '=', $siteid)
            ->update($data);
    }



    /**
     * 新增发布数量
     *
     * @param integer $id
     * @param integer $siteid
     * @return integer
     */
    public function accumulatePubliscount(int $id, int $siteid): int
    {
        return Db::table('cms_aipublish_keyword')
            ->where('id', '=', $id)
            ->where('siteid', '=', $siteid)
            ->increment('publiscount');
    }


    /**
     * 删除关键词
     *
     * @param integer $id
     * @param integer $siteid
     * @return integer
     */
    public function delete(int $id, int $siteid): int
    {
        return Db::table('cms_aipublish_keyword')
            ->where('id', '=', $id)
            ->where('siteid', '=', $siteid)
            ->delete();
    }




    /**
     * 分页查询关键词
     *
     * @param integer $siteid
     * @param boolean $reachcount 是否达到生成的数量
     * @param integer $pagesize
     * @return LengthAwarePaginator
     */
    public function paginate(
        ?int $siteid = null,
        ?bool $reachcount = null,
        string $order = 'id desc',
        int $pagesize = 20
    ): LengthAwarePaginator {
        $keywords = Db::table('cms_aipublish_keyword')
            ->where('siteid', $siteid)
            ->when($siteid !== null, function ($query) use ($siteid) {
                $query->where('siteid', $siteid);
            })
            ->orderByRaw($order)
            ->paginate($pagesize);


        //获取所有AI模型扩展
        $bllComponentExtend = new BllComponentExtend();
        $extends            = $bllComponentExtend->cascader(groupid: 3, isjson: false);
        $extends            = array_column($extends, null, 'ident');


        //栏目
        $bllCategory = new Category();
        foreach ($keywords as $key => $keyword) {


            /**格式化AI模型 */

            list($extendident, $extendclass, $extendmethor) = explode('/', $keyword->aimodel);
            $keywords[$key]->aimodeltitle = isset($extends[$extendident]) ? $extends[$extendident]['title'] : '';

            if (isset($extends[$extendident]) && $extends[$extendident]['children']) {
                $methods = $extends[$extendident]['children'];
                $methods = array_column($methods, null, 'path');
                if (isset($methods[$extendclass . '/' . $extendmethor])) {
                    $keywords[$key]->aimodeltitle .= ' / ' . $methods[$extendclass . '/' . $extendmethor]['title'];
                }
            }


            $keyword->cateancestors =  $bllCategory->getAncestorsAndSelf($keyword->cid, $keyword->siteid);
        }
        return $keywords;
    }
}
